package com.zdx.yswl.base;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.zdx.yswl.R;
import com.zdx.yswl.tool.KRandomTool;

public class KActivity extends Activity {

	protected static ArrayList<Activity> ACTIVITYS = new ArrayList<Activity>();

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 渐变背景图片失真问题
//		getWindow().setFormat(PixelFormat.RGBA_8888);
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		try {
			ACTIVITYS.add(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	public void replaceFragment(int __layoutId, android.app.Fragment __fragment) {
		
		FragmentTransaction transaction =  getFragmentManager().beginTransaction();
		transaction.setCustomAnimations(R.anim.fragment_slide_left_in, R.anim.fragment_slide_right_out);
		transaction.replace(__layoutId, __fragment).commit();
	}

	/**
	 * 创建一个空的FrameLayout
	 * 
	 * @param __context
	 * @return
	 */
	public static FrameLayout createEmptyFrameLayout(Context __context) {
		FrameLayout __layout = new FrameLayout(__context);
		__layout.setId(KRandomTool.getRandom4Int(8));
		__layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		return __layout;
	}
	
	
	protected int findIdByResourName(String __idName) {
		String __pkgName = this.getPackageName();
		return this.getResources().getIdentifier(__idName, "id", __pkgName);
	}
	
	
	
	public void hideKeyboard(View v) {
		try {
			InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			if (imm.isActive()) {
				imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	@Override
	public void finish()
	{
		try
        {
            Log.e("Finish", "" + this);
			ACTIVITYS.remove(this);
        }
        catch (Exception e)
        {
			e.printStackTrace();
		}
		super.finish();
	}

	public void exitApp() {
		Iterator<Activity> iterator = ACTIVITYS.iterator();
		while (iterator.hasNext()) {
			Activity __activity = iterator.next();
			iterator.remove();
			__activity.finish();
		}
	}
	
	//获取应用名称
	public  String getAppName(){
		return this.getResources().getString(R.string.app_name);
	}
}
